package com.polaris.live.widget.marqueen

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.ViewFlipper
import com.polaris.live.R

/**
 * 文字轮播控件【类似字幕、头条之类】
 *
 * @author xuexiang
 * @since 2019/1/14 下午10:03
 */
class MarqueeView : ViewFlipper {
    /**
     * Item翻页时间间隔【mInterval 必须大于 mAnimDuration，否则视图将会重叠】
     */
    private var mInterval = 2500

    /**
     * Item动画执行时间
     */
    private var mAnimDuration = 500

    //进出动画
    private var mAnimIn: Animation? = null
    private var mAnimOut: Animation? = null
    private var mAnimInRes: Int = R.anim.marquee_bottom_in
    private var mAnimOutRes: Int = R.anim.marquee_top_out

    constructor(context: Context?) : super(context) {
        init(null)
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init(attrs)
    }

    private fun init(attrs: AttributeSet?) {
        val a: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.MarqueeView, 0, 0)
        mInterval = a.getInt(R.styleable.MarqueeView_mq_interval, mInterval)
        mAnimInRes = a.getResourceId(R.styleable.MarqueeView_mq_animIn, mAnimInRes)
        mAnimOutRes = a.getResourceId(R.styleable.MarqueeView_mq_animOut, mAnimOutRes)
        mAnimDuration = a.getInt(R.styleable.MarqueeView_mq_animDuration, mAnimDuration)
        a.recycle()
        flipInterval = mInterval
        mAnimIn = AnimationUtils.loadAnimation(context, mAnimInRes)
        mAnimIn?.duration = mAnimDuration.toLong()
        inAnimation = mAnimIn
        mAnimOut = AnimationUtils.loadAnimation(context, mAnimOutRes)
        mAnimOut?.duration = mAnimDuration.toLong()
        outAnimation = mAnimOut
    }

    /**
     * 设置滚动数据的工厂
     * @param factory
     */
    fun <T : View?, E : Any> setMarqueeFactory(marqueeFactory: MarqueeFactory<T, E>) {
        marqueeFactory.setAttachedToMarqueeView(this)
        removeAllViews()
        val mViews: List<T>? = marqueeFactory.marqueeViews
        if (mViews != null) {
            for (i in mViews.indices) {
                addView(mViews[i])
            }
        }
    }

    /**
     * 设置翻页时间间隔
     * @param interval
     */
    fun setInterval(interval: Int) {
        mInterval = interval
        flipInterval = interval
    }

    /**
     * 设置动画执行时间
     * @param animDuration
     */
    fun setAnimDuration(animDuration: Int) {
        mAnimDuration = animDuration
        mAnimIn?.duration = animDuration.toLong()
        inAnimation = mAnimIn
        mAnimOut?.duration = animDuration.toLong()
        outAnimation = mAnimOut
    }

    fun setAnimInAndOut(animIn: Animation?, animOut: Animation?) {
        mAnimIn = animIn
        mAnimOut = animOut
        inAnimation = animIn
        outAnimation = animOut
    }

    fun setAnimInAndOut(animInId: Int, animOutID: Int) {
        mAnimIn = AnimationUtils.loadAnimation(context, animInId)
        mAnimOut = AnimationUtils.loadAnimation(context, animOutID)
        mAnimIn?.duration = mAnimDuration.toLong()
        mAnimOut?.duration = mAnimDuration.toLong()
        inAnimation = mAnimIn
        outAnimation = mAnimOut
    }

    val animIn: Animation?
        get() = mAnimIn
    val animOut: Animation?
        get() = mAnimOut

    protected override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        stopFlipping()
    }
}